﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.inspectioninfos
{
    public partial class ListDays : Web.Admin.BasePage
    {
        lcs.BLL.inspectioninfos bll = new lcs.BLL.inspectioninfos();
        lcs.BLL.inspectioninfos bu = new BLL.inspectioninfos();
        lcs.BLL.inspecplaninfo bp = new BLL.inspecplaninfo();
        int pdays = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //int i = 0;
               getDays();
                BindData();
            }
        }

        private void getDays()
        {
            int i = 0;
            DataSet ds = bp.GetAllList();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int.TryParse(ds.Tables[0].Rows[0]["ipdays"].ToString(), out i);
                if (i > 0) pdays = i;
                if (pdays == 0) pdays = 90;
            }
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }



        #region gridView

        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion

            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtdress.Text.Trim() != "")
            {
                strWhere.AppendFormat("and receive_address like '%{0}%'", txtdress.Text.Trim());
            }
            string dep = getdep();
            strWhere.AppendFormat("and equipmentManage ='{0}'", dep);
            ds = bll.GetListD(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                object obj1 = DataBinder.Eval(e.Row.DataItem, "days");
                int days = 0;
                if (obj1.ToString() != "")
                {
                    int.TryParse(obj1.ToString(), out days);
                    e.Row.Cells[7].Text = days.ToString();
                    if (pdays == 0) getDays();
                    if (days > pdays)
                        e.Row.Attributes.Add("class", "gl");
                    else
                        e.Row.Attributes.Add("style", "background:#FFF");

                }
                else
                {
                    e.Row.Cells[7].Text = "还没有巡检";
                    e.Row.Attributes.Add("class", "my");

                }

            }
            else
            {
                e.Row.Attributes.Add("style", "background:#FFF");
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

    }
}